/**
 * Copyright &copy; 2020 <a href="https://github.com/somowhere/exam-system">exam-system</a> All rights reserved.
 */
package com.shanhesoft.java.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shanhesoft.java.common.core.util.SpringContextHolder;
import com.shanhesoft.java.common.core.util.StringUtil;
import com.shanhesoft.java.common.core.vo.PageModel;
import com.shanhesoft.java.common.data.util.QueryWrapperUtil;
import com.shanhesoft.java.common.persistence.service.impl.DataServiceImpl;
import com.shanhesoft.java.modules.exam.domain.*;
import com.shanhesoft.java.modules.exam.domain.dto.*;
import com.shanhesoft.java.modules.exam.domain.vo.OjProblemsVo;
import com.shanhesoft.java.modules.exam.domain.vo.PaperVo;
import com.shanhesoft.java.modules.exam.enums.SubjectTypeEnum;
import com.shanhesoft.java.modules.exam.handler.impl.ChoicesAnswerHandler;
import com.shanhesoft.java.modules.exam.handler.impl.JudgementAnswerHandler;
import com.shanhesoft.java.modules.exam.handler.impl.MultipleChoicesAnswerHandler;
import com.shanhesoft.java.modules.exam.handler.impl.ShortAnswerHandler;
import com.shanhesoft.java.modules.exam.repository.AnswerRepository;
import com.shanhesoft.java.modules.exam.service.*;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 答题信息ServiceImpl 答题信息
 * @author admin
 * @version 2020-10-12 23:59:25
 */
@Service
@AllArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AnswerServiceImpl extends DataServiceImpl<AnswerRepository, Answer, AnswerDto, String> implements AnswerService{
	private final TBankJudgeQueService judgeQueService;
	private final TBankSingleChoiceQueService singleChoiceQueService;
	private final TBankMultipleChoiceQueService multipleChoiceQueService;
	private final ExaminationRecordService recordService;
	private final PaperQueService paperQueService;
	private final PaperService paperService;
	private final OjSubmissionsService ojSubmissionsService;
	private final OjProblemsService ojProblemsService;

	private final ChoicesAnswerHandler choicesHandler;
	private final MultipleChoicesAnswerHandler multipleChoicesHandler;
	private final JudgementAnswerHandler judgementHandler;
	private final ShortAnswerHandler shortAnswerHandler;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdate(AnswerDto answerDto, boolean submitType) {
		ExaminationRecordDto recordDto = recordService.getOneDto(answerDto.getExamRecordId());

		// 选择题、判断题自动算分(单题和总分)
		if(0 == answerDto.getQueType().intValue()) {
			TBankSingleChoiceQueDto singleChoiceQueDto = singleChoiceQueService.getOneDto(answerDto.getQueId());
			if(singleChoiceQueDto.getAnswer().equals(answerDto.getAnswer())) {
				answerDto.setAnswerType("0"); // 正确
			}
		} else if(1 == answerDto.getQueType().intValue()) {
			TBankMultipleChoiceQueDto multipleChoiceQueDto = multipleChoiceQueService.getOneDto(answerDto.getQueId());
			if(multipleChoiceQueDto.getAnswer().equals(answerDto.getAnswer())) {
				answerDto.setAnswerType("0"); // 正确
			}
		} else if(2 ==answerDto.getQueType().intValue()) {
			TBankJudgeQueDto judgeQueDto = judgeQueService.getOneDto(answerDto.getQueId());
			if(judgeQueDto.getAnswer().equals(answerDto.getAnswer())) {
				answerDto.setAnswerType("0"); // 正确
			}
		}

		if("0".equals(answerDto.getAnswerType())) {
			QueryWrapper<PaperQue> queryWrapper = new QueryWrapper<PaperQue>();
			queryWrapper.eq("paper_id", recordDto.getPaperId());
			queryWrapper.eq("que_id", answerDto.getQueId());
			queryWrapper.eq("que_type", answerDto.getQueType());
			PaperQue paperQue = paperQueService.getOne(queryWrapper);
			answerDto.setScore(paperQue.getScore());
		}

		answerDto.setEndTime(new Date());
		super.saveOrUpdate(answerDto);
		//编程题
		if(4 == answerDto.getQueType().intValue() && StringUtil.isNotBlank(answerDto.getAnswer())) {
			OjProblemsVo ojProblemsVo = ojProblemsService.findVoById(answerDto.getQueId());
			PaperVo paperVo = paperService.findUserVoById(recordDto.getPaperId());
			OjSubmissionsDto ojSubmissionsDto = new OjSubmissionsDto();
			ojSubmissionsDto.setQuestionBankId(ojProblemsVo.getQuestionBankId());
			ojSubmissionsDto.setAnswerId(answerDto.getId());
			ojSubmissionsDto.setSubmissionCode(answerDto.getAnswer());
			ojSubmissionsDto.setProblemId(ojProblemsVo.getId());
			ojSubmissionsDto.setLanguageId(ojProblemsVo.getLanguageId());
			ojSubmissionsDto.setUid(answerDto.getUserId());
			ojSubmissionsDto.setCourseId(paperVo.getCourseId());
			ojSubmissionsDto.setSubmissionSubmitTime(new Date());
			ojSubmissionsDto.setSubmissionJudgeResult("PD");
			ojSubmissionsService.saveOrUpdateReturn(ojSubmissionsDto);
		}

		// 算总分、计算正确数错误数
		if(null == recordDto.getScore()) {
			recordDto.setScore(0L);
		}
		if(null == answerDto.getScore()) {
			answerDto.setScore(0);
		}
		int oldScore = answerDto.getScore() == null ? 0 : answerDto.getScore();
		if(oldScore > 0 && recordDto.getScore() > oldScore) { // 默认认为总分大于旧得分
			recordDto.setScore(recordDto.getScore() - oldScore + answerDto.getScore());
		} else {
			recordDto.setScore(recordDto.getScore() + answerDto.getScore());
		}

		// 统计对错数
		QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<Answer>();
		answerQueryWrapper.eq("answer_type", "0");
		answerQueryWrapper.eq("exam_record_id", recordDto.getId());
		int rightCount = this.count(answerQueryWrapper);
		answerQueryWrapper = new QueryWrapper<Answer>();
		answerQueryWrapper.eq("answer_type", "1");
		answerQueryWrapper.eq("exam_record_id", recordDto.getId());
		int errorCount = this.count(answerQueryWrapper);

		recordDto.setCorrectNumber(Long.parseLong(""+rightCount));
		recordDto.setIncorrectNumber(Long.parseLong(""+errorCount));

		// 提交状态
		if(submitType) {
			recordDto.setSubmitStatus(1);
			recordDto.setEndTime(new Date());
		}

		recordService.saveOrUpdate(recordDto);
	}

	/**
	 * 分页查询考试记录
	 *
	 * @param pm 分页对象
	 * @return
	 */
	@Override
	public IPage<Answer> findPage(PageModel pm, AnswerQueryCriteria answerQueryCriteria) {
		QueryWrapper wrapper = QueryWrapperUtil.getWrapper(pm, answerQueryCriteria);
		wrapper.eq("exam_record_id", answerQueryCriteria.getExamRecordId());
		//wrapper.orderByAsc("paper_num");
		wrapper.orderByAsc("start_time");

		IPage<Answer> answerIPage = repository.selectPage(pm, wrapper);
		return answerIPage;
	}


	/**
	 * 转换实体
	 * @param answer
	 * @return
	 */
	@Override
	public AnswerDto convertAnswerEntity(Answer answer) {
		if(null == answer.getAnswer()) {
			answer.setAnswer("");
		}

		AnswerDto answerDto = new AnswerDto();
		BeanUtils.copyProperties(answer, answerDto);

		// 根据queId、queType构造一个题目对象
		SubjectDto subjectDto = new SubjectDto();
		// 判断正误
		SubjectTypeEnum subjectType = SubjectTypeEnum.matchByValue(Integer.parseInt(""+answer.getQueType()));
		if (subjectType != null) {
			switch (subjectType) {
				case CHOICES:
					TBankSingleChoiceQueDto singleChoiceQueDto = SpringContextHolder.getApplicationContext().getBean(TBankSingleChoiceQueService.class).getOneDto(answer.getQueId());
					subjectDto.setAnalysis(singleChoiceQueDto.getAnswerExplain());
					subjectDto.setAnswer(singleChoiceQueDto.getAnswer());
					//subjectDto.setChoicesType(0); // 单选
					subjectDto.setQueId(singleChoiceQueDto.getId());
					subjectDto.setQueType(subjectType.getValue());
					subjectDto.setExamRecordId(answer.getExamRecordId());
					subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
					subjectDto.setPaperId(""); // 似乎可以不用设置
					subjectDto.setQueName(singleChoiceQueDto.getContent());
					subjectDto.setScore(answer.getScore());
					// 构造option
					List<SubjectOption> singleOptionList = new ArrayList<>();
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceA())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceA());
						option.setOptionName("A");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceB())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceB());
						option.setOptionName("B");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceC())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceC());
						option.setOptionName("C");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceD())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceD());
						option.setOptionName("D");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceE())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceE());
						option.setOptionName("E");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceF())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceF());
						option.setOptionName("F");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(singleChoiceQueDto.getChoiceG())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(singleChoiceQueDto.getChoiceG());
						option.setOptionName("G");
						// 学生作答答案等于当前选项时，判断正确答案是否等于当前选项
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(singleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(singleChoiceQueDto.getId());
						singleOptionList.add(option);
					}

					subjectDto.setOptions(singleOptionList);

					choicesHandler.judgeOptionRight(answer, subjectDto);
					break;
				case MULTIPLE_CHOICES:
					TBankMultipleChoiceQueDto multipleChoiceQueDto = SpringContextHolder.getApplicationContext().getBean(TBankMultipleChoiceQueService.class).getOneDto(answer.getQueId());
					subjectDto.setAnalysis(multipleChoiceQueDto.getAnswerExplain());
					subjectDto.setAnswer(multipleChoiceQueDto.getAnswer());
					//subjectDto.setChoicesType(0); // 单选
					subjectDto.setQueId(multipleChoiceQueDto.getId());
					subjectDto.setQueType(subjectType.getValue());
					subjectDto.setExamRecordId(answer.getExamRecordId());
					subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
					subjectDto.setPaperId(""); // 似乎可以不用设置
					subjectDto.setQueName(multipleChoiceQueDto.getContent());
					subjectDto.setScore(answer.getScore());
					// 构造option
					List<SubjectOption> multipleOptionList = new ArrayList<>();
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceA())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceA());
						option.setOptionName("A");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceB())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceB());
						option.setOptionName("B");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceC())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceC());
						option.setOptionName("C");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceD())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceD());
						option.setOptionName("D");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceE())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceE());
						option.setOptionName("E");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceF())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceF());
						option.setOptionName("F");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}
					if(StringUtils.isNotEmpty(multipleChoiceQueDto.getChoiceG())) {
						SubjectOption option = new SubjectOption();
						option.setOptionContent(multipleChoiceQueDto.getChoiceG());
						option.setOptionName("G");
//								if(answer.getAnswer().equals(option.getOptionName())) {
//									option.setRight(multipleChoiceQueDto.getAnswer().equals(option.getOptionName())? TRUE : FALSE);
//								}
						option.setSubjectChoicesId(multipleChoiceQueDto.getId());
						multipleOptionList.add(option);
					}

					subjectDto.setOptions(multipleOptionList);

					multipleChoicesHandler.judgeOptionRight(answer, subjectDto);
					break;
				case JUDGEMENT:
					TBankJudgeQueDto judgeQueDto = SpringContextHolder.getApplicationContext().getBean(TBankJudgeQueService.class).getOneDto(answer.getQueId());
					subjectDto.setAnalysis(judgeQueDto.getAnswerExplain());
					subjectDto.setAnswer(judgeQueDto.getAnswer());
					//subjectDto.setChoicesType(0); // 单选
					subjectDto.setQueId(judgeQueDto.getId());
					subjectDto.setQueType(subjectType.getValue());
					subjectDto.setExamRecordId(answer.getExamRecordId());
					subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
					subjectDto.setPaperId(""); // 似乎可以不用设置
					subjectDto.setQueName(judgeQueDto.getContent());
					subjectDto.setScore(answer.getScore());
					// 构造option
					List<SubjectOption> fillOptionList = new ArrayList<>();
					SubjectOption optionRight = new SubjectOption();
					optionRight.setOptionContent("T");
					optionRight.setOptionName("");
//							if(answer.getAnswer().equals(optionRight.getOptionContent())) {
//								optionRight.setRight(judgeQueDto.getAnswer().equals(optionRight.getOptionContent())? TRUE : FALSE);
//							}
					optionRight.setSubjectChoicesId(judgeQueDto.getId());
					fillOptionList.add(optionRight);

					SubjectOption optionError = new SubjectOption();
					optionError.setOptionContent("F");
					optionError.setOptionName("");
//							if(answer.getAnswer().equals(optionError.getOptionContent())) {
//								optionError.setRight(judgeQueDto.getAnswer().equals(optionError.getOptionContent())? TRUE : FALSE);
//							}
					optionError.setSubjectChoicesId(judgeQueDto.getId());
					fillOptionList.add(optionError);

					subjectDto.setOptions(fillOptionList);

					judgementHandler.judgeRight(answer, subjectDto);
					break;
				case SHORT_ANSWER:
					TBankFillQueDto fillQueDto = SpringContextHolder.getApplicationContext().getBean(TBankFillQueService.class).getOneDto(answer.getQueId());
					subjectDto.setAnalysis(fillQueDto.getAnswerExplain());
					subjectDto.setAnswer(fillQueDto.getAnswer());
					//subjectDto.setChoicesType(0); // 单选
					subjectDto.setQueId(fillQueDto.getId());
					subjectDto.setQueType(subjectType.getValue());
					subjectDto.setExamRecordId(answer.getExamRecordId());
					subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
					subjectDto.setPaperId(""); // 似乎可以不用设置
					subjectDto.setQueName(fillQueDto.getContent());
					subjectDto.setScore(answer.getScore());

					shortAnswerHandler.judgeRight(answer, subjectDto);
					break;
				case PROBLEMS_ANSWER:
					OjProblemsDto problemsDto = SpringContextHolder.getApplicationContext().getBean(OjProblemsService.class).getOneDto(answer.getQueId());
//					QueryWrapper<OjSubmissions> wrapper = new QueryWrapper<OjSubmissions>();
//					wrapper.eq("problem_id", problemsDto.getId());
//					wrapper.eq("language_id", problemsDto.getLanguageId());
//					wrapper.eq("question_bank_id", problemsDto.getQuestionBankId());
//					OjSubmissions ojSubmissions = ojSubmissionsService.getOne(wrapper);
					subjectDto.setAnalysis(problemsDto.getProblemHint());
					subjectDto.setAnswer(problemsDto.getProblemAnswer());
					//subjectDto.setChoicesType(0); // 单选
					subjectDto.setQueId(problemsDto.getId());
					subjectDto.setQueType(subjectType.getValue());
					subjectDto.setExamRecordId(answer.getExamRecordId());
					subjectDto.setLevel(0); // 字段暂未使用， 应该从que中获取
					subjectDto.setPaperId(""); // 似乎可以不用设置
					subjectDto.setQueName(problemsDto.getProblemName());
					subjectDto.setScore(answer.getScore());

					break;
				default:
					break;
			}
		}

		answerDto.setSubject(subjectDto);
		return answerDto;
	}

	/**
	 *
	 * @param id
	 * @return
	 */
	@Override
	public AnswerDto getAnswerByRecordId(String id) {
		QueryWrapper<Answer> wrapper = new QueryWrapper<Answer>();
		wrapper.eq("exam_record_id", id);
		wrapper.orderByAsc("start_time");
		List<Answer> answerList = super.list(wrapper);
		if(null != answerList && answerList.size() > 0) {
			Answer answer = answerList.get(0);
			return convertAnswerEntity(answer);
		} else {
			return null;
		}
	}

	/**
	 * 修改单题得分、总得分(如果存在原得分，总分需减去原得分)
	 * @param answerDto
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer markAnswer(AnswerDto answerDto) {
		if(StringUtils.isEmpty(answerDto.getId())) {
			return 0;
		}

		// 保存单题答案
		Answer answer = this.getById(answerDto.getId());
		int oldScore = answer.getScore() == null ? 0 : answer.getScore();

		answer.setScore(answerDto.getScore());
		answer.setAnswerType(answerDto.getAnswerType());
		answer.setMarkStatus(answerDto.getMarkStatus());
		this.saveOrUpdate(answer);

		// 保存总得分
		ExaminationRecordDto recordDto = recordService.getOneDto(answer.getExamRecordId());
		if(null == recordDto.getScore()) {
			recordDto.setScore(0L);
		}
		if(null == answerDto.getScore()) {
			answerDto.setScore(0);
		}
		if(oldScore > 0 && recordDto.getScore() > oldScore) { // 默认认为总分大于旧得分
			recordDto.setScore(recordDto.getScore() - oldScore + answerDto.getScore());
		} else {
			recordDto.setScore(recordDto.getScore() + answerDto.getScore());
		}

		// 统计对错数
		QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<Answer>();
		answerQueryWrapper.eq("answer_type", "0");
		answerQueryWrapper.eq("exam_record_id", recordDto.getId());
		int rightCount = this.count(answerQueryWrapper);
		answerQueryWrapper = new QueryWrapper<Answer>();
		answerQueryWrapper.eq("answer_type", "1");
		answerQueryWrapper.eq("exam_record_id", recordDto.getId());
		int errorCount = this.count(answerQueryWrapper);

		recordDto.setCorrectNumber(Long.parseLong(""+rightCount));
		recordDto.setIncorrectNumber(Long.parseLong(""+errorCount));

		recordService.saveOrUpdate(recordDto);

		return 1;
	}

	/**
	 * 修改阅卷状态
	 * @param answerDto
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Integer completeMarking(AnswerDto answerDto) {
		if(StringUtils.isEmpty(answerDto.getExamRecordId())) {
			return 0;
		}
		// 保存总得分
		ExaminationRecordDto recordDto = recordService.getOneDto(answerDto.getExamRecordId());
		recordDto.setSubmitStatus(3); // 批改完成
		recordService.saveOrUpdate(recordDto);

		return 1;
	}
}
